package com.aso114.project.commonview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.aso114.project.R;

/**
 * Created by asus on 2018/11/13.
 */

public class CircleProgressView extends View {
    private int mCurrent;//当前进度
    private Paint mPaintOut;
    private Paint mPaintCurrent;
    private Paint mPaintTopText;
    private Paint mPaintDownText;
    private float mPaintWidth;//画笔宽度
    private int mPaintColor = Color.BLACK;//画笔颜色
    private int mTopTextColor = Color.BLACK;//上字体颜色
    private int mDownTextColor = 0xFF999999;//下字体颜色
    private float mTopTextSize;//上字体大小
    private float mDownTextSize;//下字体大小
    private int location;//从哪个位置开始
    private float startAngle;//开始角度
    private String mDownText = "正确率";//底部文字
    private boolean isPercent;

    private OnLoadingCompleteListener mLoadingCompleteListener;

    public CircleProgressView(Context context) {
        this(context, null);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取属性值
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        location = array.getInt(R.styleable.CircleProgressView_location, 2);
        mPaintWidth = array.getDimension(R.styleable.CircleProgressView_progress_paint_width, dip2px(context, 2));//默认4dp
        mPaintColor = array.getColor(R.styleable.CircleProgressView_progress_paint_color, mPaintColor);
        mTopTextSize = array.getDimension(R.styleable.CircleProgressView_progress_top_text_size, dip2px(context, 15));//默认18sp
        mDownTextSize = array.getDimension(R.styleable.CircleProgressView_progress_down_text_size, dip2px(context, 10));//默认18sp
        mTopTextColor = array.getColor(R.styleable.CircleProgressView_progress_top_text_color, mTopTextColor);
        mDownTextColor = array.getColor(R.styleable.CircleProgressView_progress_down_text_color, mDownTextColor);
        array.recycle();
    }

    private void initPaint() {
        //画笔->背景圆弧
        mPaintOut = new Paint();
        mPaintOut.setAntiAlias(true);
        mPaintOut.setStrokeWidth(mPaintWidth);
        mPaintOut.setStyle(Paint.Style.STROKE);
        mPaintOut.setColor(0xFFF1F1F1);
        mPaintOut.setStrokeCap(Paint.Cap.ROUND);
        //画笔->进度圆弧
        mPaintCurrent = new Paint();
        mPaintCurrent.setAntiAlias(true);
        mPaintCurrent.setStrokeWidth(mPaintWidth);
        mPaintCurrent.setStyle(Paint.Style.STROKE);
        mPaintCurrent.setColor(mPaintColor);
        mPaintCurrent.setStrokeCap(Paint.Cap.ROUND);
        //画笔->绘制字体(上)
        mPaintTopText = new Paint();
        mPaintTopText.setAntiAlias(true);
        mPaintTopText.setStyle(Paint.Style.FILL);
        mPaintTopText.setColor(mTopTextColor);
        mPaintTopText.setTextSize(mTopTextSize);
        //画笔->绘制字体（下）
        mPaintDownText = new Paint();
        mPaintDownText.setAntiAlias(true);
        mPaintDownText.setStyle(Paint.Style.FILL);
        mPaintDownText.setColor(mDownTextColor);
        mPaintDownText.setTextSize(mDownTextSize);

        if (location == 1) {//默认从左侧开始
            startAngle = -180;
        } else if (location == 2) {
            startAngle = -90;
        } else if (location == 3) {
            startAngle = 0;
        } else if (location == 4) {
            startAngle = 90;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();
        //绘制背景圆弧,因为画笔有一定的宽度，所有画圆弧的范围要比View本身的大小稍微小一些，不然画笔画出来的东西会显示不完整
        RectF rectF = new RectF(mPaintWidth / 2, mPaintWidth / 2, getWidth() - mPaintWidth / 2, getHeight() - mPaintWidth / 2);
        canvas.drawArc(rectF, 0, 360, false, mPaintOut);

        //绘制当前进度
        float sweepAngle = 360 * mCurrent / 100;
        canvas.drawArc(rectF, startAngle, sweepAngle, false, mPaintCurrent);

        //绘制进度数字
        String text = "";
        if (isPercent) {
            text = mCurrent + "%";
        } else {
            text = mCurrent + "";
        }
        //获取文字宽度
        float textWidth = mPaintTopText.measureText(text, 0, text.length());
        float tx = getWidth() / 2 - textWidth / 2;
        float baseLine = getHeight() / 2 - 1f;
        canvas.drawText(text, tx, baseLine, mPaintTopText);

        //绘制底部数字
        String dowm = mDownText;
        //获取文字宽度
        float textWidth2 = mPaintDownText.measureText(dowm, 0, dowm.length());
        float dx = getWidth() / 2 - textWidth2 / 2;
        Paint.FontMetricsInt fontMetricsInt = mPaintTopText.getFontMetricsInt();
        float dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 + fontMetricsInt.bottom;
        float baseLine2 = getHeight() / 2 + dy - 1f;
        canvas.drawText(dowm, dx, baseLine2, mPaintDownText);

        if (mLoadingCompleteListener != null && mCurrent == 100) {
            mLoadingCompleteListener.complete();
        }
    }

    /**
     * 获取进度color
     *
     * @return
     */
    public int getPaintColor() {
        return mPaintColor;
    }

    /**
     * 设置进度color并重新绘制界面
     *
     * @param mPaintColor
     */
    public void setPaintColor(int mPaintColor) {
        this.mPaintColor = mPaintColor;
        invalidate();
    }

    /**
     * 获取当前进度值
     *
     * @return
     */
    public int getCurrent() {
        return mCurrent;
    }

    /**
     * 设置当前进度并重新绘制界面
     *
     * @param mCurrent
     */
    public void setCurrent(int mCurrent, boolean isPercent) {
        this.mCurrent = mCurrent;
        this.isPercent = isPercent;
        invalidate();
    }

    /**
     * 获取当前进度值Color
     *
     * @return
     */
    public int getTopTextColor() {
        return mTopTextColor;
    }

    /**
     * 设置当前进度Color并重新绘制界面
     *
     * @param mTopTextColor
     */
    public void setTopTextColor(int mTopTextColor) {
        this.mTopTextColor = mTopTextColor;
        invalidate();
    }

    /**
     * 获取当前底部文字
     *
     * @return
     */
    public String getDownText() {
        return mDownText;
    }

    /**
     * 设置底部文字并重新绘制界面
     */
    public void setDownText(String mDownText) {
        this.mDownText = mDownText;
        invalidate();
    }

    /**
     * 获取当前底部文字color
     *
     * @return
     */
    public int getDownTextColor() {
        return mDownTextColor;
    }

    /**
     * 设置底部文字color并重新绘制界面
     */
    public void setDownTextColor(int mDownTextColor) {
        this.mDownTextColor = mDownTextColor;
        invalidate();
    }

    public void setOnLoadingCompleteListener(OnLoadingCompleteListener loadingCompleteListener) {
        this.mLoadingCompleteListener = loadingCompleteListener;
    }

    public interface OnLoadingCompleteListener {
        void complete();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
